package com.esun.cheetah.view.setting;

import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.esun.cheetah.R;
import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.Language;
import com.esun.cheetah.view.HomeAct;

public class ChooseLanguageAct extends Activity implements OnClickListener {
	private CheckBox cbSimple;
	private CheckBox cbHant;
	private Button btnGoScore;
	private static UserInfo userInfo = UserInfo.getUserInfo();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.choose_language);
		initViews();
	}

	private void initViews() {
		cbSimple = (CheckBox) findViewById(R.id.cbSim);
		cbHant = (CheckBox) findViewById(R.id.cbHant);
		btnGoScore = (Button) findViewById(R.id.btnGoScore);
		btnGoScore.setOnClickListener(this);
		verify();
		cbSimple.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					cbHant.setChecked(false);
					userInfo.setChooseTime(0L);
					userInfo.setValidTime(0l);
					
				}else{
					userInfo.setValidTime(validTime());
					
				}

			}
		});

		cbHant.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked) {
					cbSimple.setChecked(false);
					userInfo.setChooseTime(System.currentTimeMillis());
					userInfo.setValidTime(validTime());
				}else{
					userInfo.setValidTime(0l);
				}
			}
		});
	}

	/**
	 * 检测是繁体还是简体
	 */
	private void verify() {
		boolean simpleOrHan = userInfo.isTypeface();
		if (simpleOrHan) {
			cbSimple.setChecked(true);
		} else {
			cbHant.setChecked(true);
		}
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnGoScore:
			finish();
			break;
		default:
			break;
		}
	}
	@Override
	protected void onPause() {
		super.onPause();
		Intent mIntent = new Intent(this, HomeAct.class);
		HomeAct.flag=Language.FLAG_ZERRO;
		startActivity(mIntent);
	
	}
	private Long validTime(){
		Calendar calendar=Calendar.getInstance();  
		int hour=calendar.get(Calendar.HOUR_OF_DAY);
		Long longHour=(long) (hour*60*60*1000);
		int minute=calendar.get(Calendar.MINUTE);
		Long minuteTime=(long) (minute*60*1000);
		int second=calendar.get(Calendar.SECOND);
		Long secondTime=(long) (second*1000);
		int millisecond=calendar.get(Calendar.MILLISECOND);
		return 86400000-(longHour+minuteTime+secondTime+millisecond);
		
	}
}
